package kr.hkit.androidcomplete03.ch281;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import kr.hkit.androidcomplete03.R;
import kr.hkit.androidcomplete03.ch281.common.DaeguNoticeAdapter;
import kr.hkit.androidcomplete03.ch281.common.DaeguNoticeItem;
import kr.hkit.androidcomplete03.ch281.common.Item;
import kr.hkit.androidcomplete03.ch281.common.MyAdapter;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

public class DaeguNotice extends Activity {
	ProgressDialog progressDlg;
	ListView list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch281_parser);
		list = (ListView) findViewById(R.id.ch281_parser_listview);

		new ParseXml().execute("http://www.daegu.go.kr/rss/notice.xml");
	}

	private class ParseXml extends
			AsyncTask<String, Void, ArrayList<DaeguNoticeItem>> {
		@Override
		protected void onPreExecute() {
			progressDlg = ProgressDialog.show(DaeguNotice.this, "Wait",
					"DaeguNoticeList Making. . . ");
		}

		@Override
		protected ArrayList<DaeguNoticeItem> doInBackground(String... params) {
			String str = null;
			StringBuilder builder = null;

			try {
				URL url = new URL(params[0]);
				HttpURLConnection http = (HttpURLConnection) url.openConnection();
				http.setDefaultUseCaches(false);
				http.setDoInput(true);
				http.setDoOutput(true);
				http.setRequestMethod("POST");

				InputStreamReader tmp = new InputStreamReader(http.getInputStream(), "UTF8");
				BufferedReader reader = new BufferedReader(tmp);
				builder = new StringBuilder();

				while ((str = reader.readLine()) != null) {
					builder.append(str);
					Toast.makeText(DaeguNotice.this, str, 0).show();
				}
				tmp.close();
				reader.close();
				http.disconnect();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return parsingXml(builder.toString());
		}

		private ArrayList<DaeguNoticeItem> parsingXml(String xml) {
			ArrayList<DaeguNoticeItem> arItems = new ArrayList<DaeguNoticeItem>();
			DaeguNoticeItem itemClass;

			try {
				JSONArray ja = new JSONArray(xml);
				for (int i = 0; i < ja.length(); i++) {
					JSONObject order = ja.getJSONObject(i);
					itemClass = new DaeguNoticeItem();
					itemClass.setTitle(order.getString("title"));
					itemClass.setSubTitle(order.getString("description"));
					itemClass.setUrl(order.getString("link"));
					arItems.add(itemClass);
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return arItems;
		}

		protected void onPostExecute(ArrayList<DaeguNoticeItem> result) {
			DaeguNoticeAdapter adapter = new DaeguNoticeAdapter(
					DaeguNotice.this, result);
			progressDlg.dismiss();
			list.setAdapter(adapter);
		}
	}
}
